home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / GS < prev    next >
Text File  |  1991-10-26  |  10KB  |  340 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gs.c */
  21. /* Driver program for Ghostscript */
  22. #include <stdio.h>
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "alloc.h"
  26. #include "estack.h"
  27. #include "ostack.h"
  28. #include "store.h"
  29. #include "stream.h"
  30.  
  31. /* Library routines not declared in a standard header */
  32. extern char *getenv(P1(const char *));
  33.  
  34. /* Exported data */
  35. uint memory_chunk_size = 20000;
  36. /* File name search paths */
  37. char **gs_lib_paths;
  38. private int gs_lib_count;
  39. private char *gs_lib_env_path;
  40.  
  41. /* Configuration information imported from gconfig.c. */
  42. extern char *gs_lib_default_path;
  43. extern char *gs_init_file;
  44.  
  45. /* Device procedures imported from gsdevice.c. */
  46. typedef struct gx_device_s gx_device;
  47. extern gx_device *gs_getdevice(P1(int));
  48. extern char *gs_devicename(P1(gx_device *));
  49.  
  50. /* Help string */
  51. private char *gs_help1 = "\
  52. Usage: gs [switches] [file1.ps file2.ps ...]\n\
  53.    or  gs [switches] [file1.ps ...] -- filen.ps arg1 arg2 ...\n\
  54. The latter passes arg1 ... to the program in filen.ps.\n\
  55. Available devices:";
  56. private char *gs_help2 = "\n\
  57. Switches:\n\
  58.     -d<name>[=<token>]   define name as token, or null if no token given\n\
  59.     -D<name>[=<token>]   same as -d\n\
  60.     -g<width>x<height>   set width and height (`geometry') for device\n\
  61.     -I<prefix>           add prefix to search path\n\
  62.     -q                   `quiet' mode, suppress most messages\n\
  63.     -s<name>=<string>    define name as string\n\
  64.     -S<name>=<string>    same as -s\n\
  65.     -sDEVICE=<devname>   select initial device\n\
  66. `-' alone as a file name means read from stdin non-interactively.\n\
  67. For more information, please read the use.doc file.\n";
  68.  
  69. /* Forward references */
  70. private void runarg(P4(char **, char *, char *, int));
  71. private void run_string(P1(char *));
  72. private void init1(), init2();
  73. private void set_lib_paths();
  74. private void run_file(P2(char *file_name, int user_errors));
  75. private void debug_dump_stack(P1(int code));
  76.  
  77. /* Parameters set by swproc */
  78. private int user_errors;
  79. private int quiet;
  80. private int batch;
  81.  
  82. /* Static versions of argc and argv (for -- only) */
  83. private int static_argc;
  84. private char **static_argv;
  85.  
  86. main(int argc, char *argv[])
  87. {    int num_files;
  88.     int swproc(P2(char **, char *));
  89.     void argproc(P2(char **, int));
  90.     static_argc = argc;
  91.     static_argv = argv;
  92.     /* Do platform-dependent initialization. */
  93.     /* We have to do this as the very first thing, */
  94.     /* because it detects attempts to run 80N86 executables (N>0) */
  95.     /* on incompatible processors. */
  96.     gp_init();
  97.     /* Initialize the file search paths */
  98.     gs_lib_env_path = 0;
  99.        {    char *lib = getenv("GS_LIB");
  100.         if ( lib != 0 ) 
  101.            {    int len = strlen(lib);
  102.             gs_lib_env_path = gs_malloc(len + 1, 1, "GS_LIB");
  103.             strcpy(gs_lib_env_path, lib);
  104.            }
  105.        }
  106.     gs_lib_paths =
  107.         (char **)gs_malloc(argc + 2, sizeof(char *), "-I array");
  108.     gs_lib_count = 0;
  109.     set_lib_paths();
  110.     /* Execute files named in the command line, */
  111.     /* processing options along the way. */
  112.     /* Wait until the first file name (or the end */
  113.     /* of the line) to finish initialization. */
  114.     batch = 0;
  115.     quiet = 0;
  116.     user_errors = 1;
  117.     num_files = gs_main(argc, argv, "GS.MAP", swproc, argproc);
  118.     if ( num_files == 0 )
  119.        {    init2();
  120.        }
  121.     if ( !batch ) run_string("start");
  122.     gs_exit(0);
  123. }
  124.  
  125. /* Process switches */
  126. int
  127. swproc(char **swp, char *arg)
  128. {    char sw = (*swp)[1];
  129.     switch ( sw )
  130.        {
  131.     default:
  132.         return -1;
  133.     case 0:                /* read stdin as a file */
  134.         batch = 1;
  135.         init2();        /* Finish initialization */
  136.         run_string("(%stdin) (r) file cvx execute");
  137.         break;
  138.     case '-':            /* run with command line args */
  139.         runarg(swp + 1, "{userdict /ARGUMENTS [", "] put (",
  140.                static_argv + static_argc - (swp + 2));
  141.         gs_exit(0);
  142.     case 'h':            /* print help */
  143.     case '?':
  144.         fputs(gs_help1, stdout);
  145.            {    int i;
  146.             gx_device *pdev;
  147.             for ( i = 0; (pdev = gs_getdevice(i)) != 0; i++ )
  148.                 printf(" %s", gs_devicename(pdev));
  149.            }
  150.         fputs(gs_help2, stdout);
  151.         gs_exit(0);
  152.     case 'I':            /* specify search path */
  153.         gs_lib_paths[gs_lib_count] = arg;
  154.         gs_lib_count++;
  155.         set_lib_paths();
  156.         break;
  157.     case 'q':            /* quiet startup */
  158.        {    ref vnull;
  159.         quiet = 1;
  160.         init1();
  161.         make_null(&vnull);
  162.         initial_enter_name("QUIET", &vnull);
  163.        }    break;
  164.     case 'D':            /* define name */
  165.     case 'd':
  166.     case 'S':            /* define name as string */
  167.     case 's':
  168.        {    char *eqp = strchr(arg, '=');
  169.         ref value;
  170.         /* Initialize the object memory, scanner, and */
  171.         /* name table now if needed. */
  172.         init1();
  173.         if ( eqp == NULL )
  174.            {    make_null(&value);
  175.            }
  176.         else if ( eqp == arg )
  177.            {    printf("Usage: -dname, -dname=token, -sname=string");
  178.             gs_exit(1);
  179.            }
  180.         else
  181.            {    int code;
  182.             *eqp++ = 0;    /* delimit name */
  183.             if ( sw == 'D' || sw == 'd' )
  184.                {    stream astream;
  185.                 sread_string(&astream,
  186.                          (byte *)eqp, strlen(eqp));
  187.                 code = scan_token(&astream, 0, &value);
  188.                 if ( code )
  189.                    {    printf("-dname= must be followed by a valid token");
  190.                     gs_exit(1);
  191.                    }
  192.                }
  193.             else
  194.                {    int len = strlen(eqp);
  195.                 char *str = gs_malloc((uint)len, 1, "-s");
  196.                 if ( str == 0 )
  197.                    {    lprintf("Out of memory!\n");
  198.                     gs_exit(1);
  199.                    }
  200.                 strcpy(str, eqp);
  201.                 make_tasv(&value, t_string, a_read+a_execute,
  202.                       len, bytes, (byte *)str);
  203.                }
  204.            }
  205.         /* Enter the name in systemdict */
  206.         initial_enter_name(arg, &value);
  207.         break;
  208.        }
  209.     case 'g':            /* define device geometry */
  210.        {    long width, height;
  211.         ref value;
  212.         init1();
  213.         if ( sscanf(arg, "%ldx%ld", &width, &height) != 2 )
  214.            {    printf("-g must be followed by <width>x<height>\n");
  215.             gs_exit(1);
  216.            }
  217.         make_int(&value, width);
  218.         initial_enter_name("DEVICEWIDTH", &value);
  219.         make_int(&value, height);
  220.         initial_enter_name("DEVICEHEIGHT", &value);
  221.         break;
  222.        }
  223.     case 'M':            /* set memory allocation increment */
  224.        {    unsigned msize = 0;
  225.         sscanf(arg, "%d", &msize);
  226.         if ( msize <= 0 || msize >= 64 )
  227.            {    printf("-M must be between 1 and 64");
  228.             gs_exit(1);
  229.            }
  230.         memory_chunk_size = msize << 10;
  231.        }
  232.         break;
  233.        }
  234.     return 0;
  235. }
  236.  
  237. /* Process file names */
  238. void
  239. argproc(char **argp, int index)
  240. {    runarg(argp, "{", "(", 0);
  241. }
  242. private void
  243. runarg(char **argp, char *pre, char *post, int nstrs)
  244. {    char *arg = *argp;
  245.     static char *pex = ")run}execute";
  246.     int len = strlen(pre) + strlen(arg) + strlen(post) + strlen(pex) + 1;
  247.     char *line;
  248.     int i;
  249.     for ( i = 1; i <= nstrs; i++ )
  250.         len += strlen(argp[i]) + 2;
  251.     init2();    /* Finish initialization */
  252.     line = gs_malloc(len, 1, "argproc");
  253.     strcpy(line, pre);
  254.     for ( i = 1; i <= nstrs; i++ )
  255.        {    strcat(line, "(");
  256.         strcat(line, argp[i]);
  257.         strcat(line, ")");
  258.        }
  259.     strcat(line, post);
  260.     strcat(line, arg);
  261.     strcat(line, pex);
  262.     run_string(line);
  263. }
  264. private void
  265. run_string(char *str)
  266. {    int code;
  267.     ref stref;
  268.     make_tasv(&stref, t_string, a_executable + a_read + a_execute,
  269.           strlen(str), bytes, (byte *)str);
  270.     code = interpret(&stref, user_errors);
  271.     zflush((ref *)0);    /* flush stdout */
  272.     zflushpage((ref *)0); /* force display update */
  273.     if ( code ) debug_dump_stack(code), gs_exit(2);
  274. }
  275.  
  276. private int init1_done = 0, init2_done = 0;
  277. private void
  278. init1()
  279. {    if ( !init1_done )
  280.        {    alloc_init(gs_malloc, gs_free, memory_chunk_size);
  281.         name_init();
  282.         obj_init();        /* requires name_init */
  283.         scan_init();        /* ditto */
  284.         init1_done = 1;
  285.        }
  286. }
  287. private void
  288. init2()
  289. {    init1();
  290.     if ( !init2_done )
  291.        {    gs_init();
  292.         zop_init();
  293.         interp_init(1);        /* requires obj_init */
  294.         op_init();        /* requires obj_init, scan_init */
  295.         /* Execute the standard initialization file. */
  296.         run_file(gs_init_file, user_errors);
  297.         init2_done = 1;
  298.        }
  299.    }
  300.  
  301. /* Complete the list of library search paths. */
  302. private void
  303. set_lib_paths()
  304. {    char **ppath = &gs_lib_paths[gs_lib_count];
  305.     if ( gs_lib_env_path != 0 ) *ppath++ = gs_lib_env_path;
  306.     if ( gs_lib_default_path != 0 ) *ppath++ = gs_lib_default_path;
  307.     *ppath = 0;
  308. }
  309.  
  310. /* Open and execute a file */
  311. private void
  312. run_file(char *file_name, int user_errors)
  313. {    ref initial_file;
  314.     int code;
  315.     if ( lib_file_open(file_name, strlen(file_name), &initial_file) < 0 )
  316.        {    eprintf1("Can't find initialization file %s\n", file_name);
  317.         gs_exit(1);
  318.        }
  319.     r_set_attrs(&initial_file, a_execute + a_executable);
  320.     code = interpret(&initial_file, user_errors);
  321.     if ( code < 0 )
  322.         debug_dump_stack(code), gs_exit(1);
  323. }
  324.  
  325. /* Debugging code */
  326. extern void debug_print_ref(P1(ref *));
  327. extern void debug_dump_refs(P3(ref *, ref *, char *));
  328. extern ref error_object;
  329.  
  330. /* Dump the stacks after interpretation */
  331. private void
  332. debug_dump_stack(int code)
  333. {    zflush(osp);    /* force out buffered output */
  334.     dprintf1("\nUnexpected interpreter error %d!\nError object: ", code);
  335.     debug_print_ref(&error_object);
  336.     dputc('\n');
  337.     debug_dump_refs(osbot, osp + 1, "Operand stack");
  338.     debug_dump_refs(esbot, esp + 1, "Execution stack");
  339. }
  340.